package com.study.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.xml.ws.spi.http.HttpContext;

import org.aspectj.internal.lang.annotation.ajcDeclareAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.OpportunityMapper;
import com.study.model.mdao.OrderFormMapper;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Dictionary;
import com.study.model.pojo.Linkman;
import com.study.model.pojo.Orderform;
import com.study.model.pojo.User;
import com.study.model.vo.CartBox;
import com.study.services.AddresdetailService;
import com.study.services.CustomerService;
import com.study.services.DepotgoodsshelfService;
import com.study.services.DictionaryService;
import com.study.services.GoodsService;
import com.study.services.MoneyBackPlanService;
import com.study.services.OrderService;
import com.study.services.OrderfromServices;
import com.study.services.UserService;
import com.study.util.NumberForm;

/**
 * 订单控制层
 * 
 * @author 郭星
 *
 */
@Controller
public class OrderController {

	@Autowired
	LogforcustDAO logforcustDAO;
	@Autowired
	OrderService service;
	@Autowired
	OrderFormMapper orderFormMapper;

	@Autowired
	UserService userservice;
	@Autowired
	DictionaryService dicservice;
	@Autowired
	AddresdetailService addresdetailService;
	@Autowired
	CustomerService customerservice;
	@Autowired
	GoodsService goodsService;
	@Autowired
	MoneyBackPlanService moneybackplanservice;
	@Autowired
	OpportunityMapper oppmapper;
	@Autowired
	OrderfromServices orderfromServices;
	@Autowired
	DepotgoodsshelfService depotgoodService;

	/**
	 * 访问地址http://localhost:8089/createOrder
	 * 
	 * @return
	 *
	 */
	@RequestMapping("/createOrder")
	public String first(HttpSession session) {
		// List<Orderform> list = service.selectByPager(1, 8);
		// session.setAttribute("DICTIONARY43", list);
		return "onSale/createOrder";
	}

	/**
	 * 传入ifram表单信息
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/orderForm")
	public ModelAndView first2(HttpSession session) {
		ModelAndView mv = new ModelAndView("/onSale/createOrderform2");
		System.out.println("orderForm:iframe");
	
		return mv;
	}

	/**
	 * 传入ifram表单信息
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/orderForm2")
	public ModelAndView first3(Integer id, HttpSession session) {
		System.out.println("进来了修改订单");
		System.out.println(id);
		ModelAndView mv = new ModelAndView("/onSale/createOrderform3");
		if (id != null) {
			int linid = id.intValue();
			JSONObject jsonObject = new JSONObject();
			jsonObject = service.findById(linid);
			mv.addObject("orderUpdate", jsonObject);

			
			System.out.println("orderForm:iframe");

		}
		
		return mv;
	}

	/**
	 * 分页查询
	 */
	@PostMapping("/onSale/createOrderse")
	@ResponseBody
	public String getForm1(HttpSession session) {
		System.out.println(session.getAttribute("useid"));
		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = service.selectByPagerSql("", "", "", 1, 10, uid);
		String json ="";
		if (list != null && list.size() > 0) {

			formatDate(list);
			for (JSONObject jsonObject : list) {
				jsonObject.put("huikuan", service.huikuan(jsonObject.getInteger("o_id")));
				jsonObject.put("yeshuikuan", service.yeshuikuan(jsonObject.getInteger("o_id")));
				// System.out.println("第一边查询：" + jsonObject);
			}
			PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

			json = JSONObject.toJSONString(pager);
		}

	

		return json;
	}

	/**
	 * 处理list集合中的时间格式
	 * 
	 * @param list
	 */
	public static void formatDate(List<JSONObject> list) {

		int i = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		if (list != null && list.size() > 0) {
			for (JSONObject jsonObject : list) {
				// System.out.println(jsonObject);
				String string = jsonObject.getString("o_date");
				if (string != null && string.length() > 0) {
					try {

						Date parse = format.parse(string);
						// System.out.println(format.format(parse));
						list.get(i).put("o_date", format.format(parse).toString());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				i++;

			}
		}

	}

	/**
	 * oreder分页、全部、带条件查询
	 * 
	 * @param selects     搜索框条件头
	 * @param selectinput 搜索框条件中
	 * @param selectend   搜索框条件尾
	 * @param currentPage 当前页
	 * @param pagesize    页大小
	 * @return
	 */
	@PostMapping("/onSale/createOrderseQuery")
	@ResponseBody
	public String getForm3(String selects, String selectinput, String selectend, int currentPage, int pagesize,
			HttpSession session) {
		System.out.println(currentPage + "currentPage");
		System.out.println(pagesize + "pagesize");
		System.out.println(selectinput);
		System.out.println(selects);
		if (selects.equals("全部数据")) {
			selects = null;
		}
		String json ="";
		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = service.selectByPagerSql(selects, selectinput, selectend, currentPage, pagesize, uid);

		if (list != null && list.size() > 0) {
			formatDate(list);
			for (JSONObject jsonObject : list) {
				jsonObject.put("huikuan", service.huikuan(jsonObject.getInteger("o_id")));
				jsonObject.put("yeshuikuan", service.yeshuikuan(jsonObject.getInteger("o_id")));
				// System.out.println("第一边查询：" + jsonObject);
			}
			PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

			json = JSONObject.toJSONString(pager);
		}

		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		json = JSONObject.toJSONString(pager);
		return json;
	}

	/**
	 * 添加一条订单概述信息
	 * 
	 * @param custid      客户id
	 * @param titles      标题
	 * @param costomers   客户
	 * @param orderid     订单号
	 * @param cartyrs     类别
	 * @param paytypes    支付类别
	 * @param opportunity 销售机会
	 * @param qmans       签约人
	 * @param givemansto  收货人
	 * @param havetime    发货时间
	 * @param haveoldtime 最晚发货时间
	 * @param endmoney    结款方式
	 * @param fills       员工
	 * @return
	 */
	@PostMapping("addOrderFromItem")
	@ResponseBody
	public String addForm(String custid, String titles, String costomers, String cartyrs, String paytypes,
			String opportunity, String qmans, String givemansto, Date havetime, Date haveoldtime, String endmoney,
			String users, HttpSession session) {
		ModelAndView mv = new ModelAndView("/error");
		System.out.println("我来了Post");

		System.out.println("标题:" + titles + " 客户:" + costomers + " 类别:" + cartyrs + " 支付类型：" + paytypes + " 销售机会:"
				+ opportunity + " 签约人:" + qmans + " 收货人:" + givemansto + " 发货时间:" + havetime + " 最晚发货时间:" + haveoldtime
				+ " 员工sss：" + users);

		Customer ds = customerservice.selectCustomerByName(costomers);

		int result = 0;

		if (opportunity != null && opportunity.length() > 0) {
			result = service.addOrderMapper(titles, ds.getCusId(), 0, Integer.parseInt(cartyrs),
					Integer.parseInt(paytypes), Integer.parseInt(opportunity), Integer.parseInt(qmans),
					Integer.parseInt(givemansto), havetime, haveoldtime, Integer.parseInt(endmoney),
					Integer.parseInt(users));

			System.out.println("最后的id：" + result);// Integer.parseInt(opportunity)
			session.setAttribute("orderidsTwo", result);

			if (result > 0) {
				// mv = new ModelAndView("/getCustomer/huokexiansuo");
				System.out.println("成功");
			} else {
				// mv = new ModelAndView("/error");
				System.out.println("失败");
			}
		} else {
			result = service.addOrderMapper(titles, ds.getCusId(), 0, Integer.parseInt(cartyrs),
					Integer.parseInt(paytypes), null, Integer.parseInt(qmans), Integer.parseInt(givemansto), havetime,
					haveoldtime, Integer.parseInt(endmoney), Integer.parseInt(users));

			System.out.println("最后的id：" + result);
			session.setAttribute("orderidsTwo", result);

			if (result > 0) {
				// mv = new ModelAndView("/getCustomer/huokexiansuo");

				System.out.println("成功");
			} else {
				// mv = new ModelAndView("/error");
				System.out.println("失败");
			}
		}
		// 查看客户的状态
		JSONObject js = customerservice.selectStatus(custid);
		// 将客户状态改为签约客户
		if (result > 0 && js.getInteger("cus_status") != 1123) {
			customerservice.updatestate(custid);
		}

		return result + "";
	}

	/**
	 * 修改一条订单概述信息
	 * 
	 * @param titles      标题
	 * @param costomers   客户
	 * @param orderid     订单号
	 * @param cartyrs     类别
	 * @param paytypes    支付类别
	 * @param opportunity 销售机会
	 * @param qmans       签约人
	 * @param givemansto  收货人
	 * @param havetime    发货时间
	 * @param haveoldtime 最晚发货时间
	 * @param endmoney    结款方式
	 * @param fills       员工
	 * @return
	 */
	@PostMapping("updateOrderFromItemse")
	@ResponseBody
	public JSONObject updateForm(String oids, String radio2, String titles, String costomers, String orderid,
			String cartyrs, String paytypes, String opportunity, String qmans, String givemansto, Date havetime,
			Date haveoldtime, String endmoney, String fills) {
		// ModelAndView mv = new ModelAndView("/error");
		System.out.println("我来了Post");
		// User user = userservice.queryByName("李四");

		System.out.println("标题:" + titles + " 客户:" + costomers + " 订单号:" + orderid + " 类别:" + cartyrs + " 支付类型："
				+ paytypes + " 销售机会:" + opportunity + " 签约人:" + qmans + " 收货人:" + givemansto + " 发货时间:" + havetime
				+ " 最晚发货时间:" + haveoldtime + " 订单id：" + oids + " 状态" + radio2);

		Customer ds = customerservice.selectCustomerByName(costomers);

		if (opportunity != null && opportunity.length() > 0) {
			boolean result = service.updateOrderMapper(Integer.parseInt(oids), Integer.parseInt(radio2), titles,
					ds.getCusId(), Integer.parseInt(orderid), Integer.parseInt(cartyrs), Integer.parseInt(paytypes),
					null, Integer.parseInt(qmans), Integer.parseInt(givemansto), havetime, haveoldtime,
					Integer.parseInt(endmoney), Integer.parseInt(fills));
			if (result) {
				// mv = new ModelAndView("/getCustomer/huokexiansuo");
				System.out.println("成功");
			} else {
				// mv = new ModelAndView("/error");
				System.out.println("失败");
			}
		} else {
			boolean result = service.updateOrderMapper(Integer.parseInt(oids), Integer.parseInt(radio2), titles,
					ds.getCusId(), Integer.parseInt(orderid), Integer.parseInt(cartyrs), Integer.parseInt(paytypes),
					null, Integer.parseInt(qmans), Integer.parseInt(givemansto), havetime, haveoldtime,
					Integer.parseInt(endmoney), null);
			if (result) {
				// mv = new ModelAndView("/getCustomer/huokexiansuo");
				System.out.println("成功");
			} else {
				// mv = new ModelAndView("/error");
				System.out.println("失败");
			}
		}

		JSONObject jsd = new JSONObject();
		return jsd;
	}

	/**
	 * 通过名称查询对应客户
	 * 
	 * @return
	 * @return
	 * 
	 * @return
	 */
	@PostMapping("/onSale/findByCoustomerName")
	@ResponseBody
	public JSONObject findByCoustomer(Integer custid, HttpSession session) {
		System.out.println(custid);
		List<Linkman> selectByName = customerservice.selectById(custid);
		JSONObject jso = new JSONObject();
		jso.put("custo", selectByName);// 放入联系人
		jso.put("opp", oppmapper.findByCusId(custid));
		return jso;
	}

	/**
	 * 访问地址http://localhost:8089/createOrder 跳转到第二个界面 订单详情页面
	 * 
	 * @return
	 *
	 */
	@RequestMapping("shozhongordertwo")
	public ModelAndView twos(HttpSession session) {
		// List<Orderform> list = service.selectByPager(1, 8);
		// session.setAttribute("DICTIONARY43", list);
		int attribute = (int) session.getAttribute("orderidsTwo");
		ModelAndView mv = new ModelAndView("onSale/shozhongordertwo");

		JSONObject findById = service.findById(attribute);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String string = findById.getString("o_date");
		String string2 = findById.getString("o_latedate");

		Date parse;
		try {
			if (string.length() > 0 && string != null) {
				parse = format.parse(string);
				Date parse2 = format.parse(string2);
				System.out.println(parse);
				System.out.println(parse2);
				findById.put("o_date", format.format(parse).toString());
				findById.put("o_latedate", format.format(parse2).toString());
				System.out.println(findById);
			}
			mv.addObject("ordertotext", findById);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 查询当前订单之前的订单明细
		CartBox cart = (CartBox) session.getAttribute("CartBoxAll");
		if (cart == null) {
			cart = new CartBox();
		}
		service.findOrderDetail(attribute, cart);
		System.out.println("超级开始的购物车：" + cart);
		session.setAttribute("ingOid", attribute);
		JSONObject jsonObj = (JSONObject) JSON.toJSON(cart);
		// 获取中文金额
		StringBuffer test = NumberForm.test(jsonObj.getInteger("price"));
		System.out.println("这次的总金额是：" + test);
		jsonObj.put("chinas", test);
		// System.out.println("bean2Json()方法：jsonObj==" + jsonObj);
		session.setAttribute("CartBoxAll", cart);
		session.setAttribute("CartBoxAllJson", jsonObj);

		// 查询回款记录
		// System.out.println(attribute);
		JSONObject findByOidItem = moneybackplanservice.findByOidItem(attribute);
		session.setAttribute("backItem", findByOidItem);
		return mv;
	}

	/**
	 * 跳转到第二个界面
	 * 
	 * @param o_id
	 * @param session
	 * @return
	 */
	@RequestMapping("shozhongordertwo2")
	public ModelAndView twos2(Integer oid, HttpSession session) {
		// List<Orderform> list = service.selectByPager(1, 8);
		// session.setAttribute("DICTIONARY43", list);
		session.setAttribute("orderidsTwo", oid);
		int attribute = oid;
		ModelAndView mv = new ModelAndView("onSale/shozhongordertwo");

		JSONObject findById = service.findById(attribute);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String string = findById.getString("o_date");
		String string2 = findById.getString("o_latedate");

		Date parse;
		try {
			if (string != null && string.length() > 0) {

				if (string2 != null && string2.length() > 0) {
					parse = format.parse(string);
					Date parse2 = format.parse(string2);
					// System.out.println(parse);
					// System.out.println(parse2);
					findById.put("o_date", format.format(parse).toString());
					findById.put("o_latedate", format.format(parse2).toString());
					// System.out.println(findById);
				}
			}

			mv.addObject("ordertotext", findById);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 查询当前订单之前的订单明细
		CartBox cart = (CartBox) session.getAttribute("CartBoxAll");
		if (cart == null) {
			cart = new CartBox();
		}
		service.findOrderDetail(oid, cart);
		System.out.println("超级开始的购物车：" + cart);
		session.setAttribute("ingOid", oid);
		JSONObject jsonObj = (JSONObject) JSON.toJSON(cart);
		// System.out.println("bean2Json()方法：jsonObj==" + jsonObj);
		// 获取中文金额
		StringBuffer test = NumberForm.test(jsonObj.getInteger("price"));
		// System.out.println("这次的总金额是：" + test);
		jsonObj.put("chinas", test);
		session.setAttribute("CartBoxAll", cart);
		session.setAttribute("CartBoxAllJson", jsonObj);

		// 查询回款计划
		// System.out.println(oid);
		JSONObject findByOidItem = moneybackplanservice.findByOidItem(attribute);
		session.setAttribute("backItem", findByOidItem);
		// System.out.println("到这里了" + findByOidItem);

		// 查询回款记录

		return mv;
	}

	/**
	 * 跳转到订单明细界面
	 * 
	 * @param oid
	 * @param session
	 * @return
	 */
	@RequestMapping("shozhongorderdetail")
	public ModelAndView goodsMx(Integer oid, HttpSession session) {
		// System.out.println("oid" + oid);
		ModelAndView mv = new ModelAndView("onSale/shozhongorderdetail");
		CartBox cart = (CartBox) session.getAttribute("CartBoxAll");
		if (cart == null) {
			cart = new CartBox();
		}
		service.findOrderDetail(oid, cart);
		// System.out.println("刚开始的购物车：" + cart);
		session.setAttribute("ingOid", oid);
		JSONObject jsonObj = (JSONObject) JSON.toJSON(cart);
		// System.out.println("bean2Json()方法：jsonObj==" + jsonObj);
		session.setAttribute("CartBoxAll", cart);
		session.setAttribute("CartBoxAllJson", jsonObj);
		mv.addObject("oids", oid);
		return mv;
	}

	/**
	 * 添加一个商品到订单明细中
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("addGoodToCart")
	@ResponseBody
	public CartBox addGoods(int ids, HttpSession session) {
		// System.out.println("oid" + ids);
		CartBox cart = (CartBox) session.getAttribute("CartBoxAll");
		if (cart == null) {
			cart = new CartBox();
		}
		cart.addProduct(goodsService.findByGoodsIdByVo(ids));
		// System.out.println("当前购物车...：" + cart.getMap());
		// System.out.println("service");
		session.setAttribute("CartBoxAll", cart);
		return cart;
	}

	/**
	 * 修改商品数量
	 * 
	 * @param ids
	 * @param quatitys
	 * @param session
	 * @return
	 */
	@PostMapping("updateGoodToCart")
	@ResponseBody
	public CartBox updateGoodsQuatitys(int ids, int quatitys, HttpSession session) {

		System.out.println("oid：" + ids + " quatitys：" + quatitys);
		CartBox cart = (CartBox) session.getAttribute("CartBoxAll");
		cart.modifyproductQuatity(ids, quatitys, cart);

		// System.out.println("修改后当前购物车：" + cart.getMap());
		session.setAttribute("CartBoxAll", cart);
		return cart;
	}

	/**
	 * 删除商品信息
	 * 
	 * @param ids
	 * @param session
	 * @return
	 */
	@PostMapping("deleteGoodToCart")
	@ResponseBody
	public CartBox deleteGoods(int ids, HttpSession session) {
		System.out.println("oid" + ids);
		CartBox cart = (CartBox) session.getAttribute("CartBoxAll");
		cart.deleteProduct(ids, cart);
		// System.out.println("当前购物车：" + cart.getMap());
		session.setAttribute("CartBoxAll", cart);
		return cart;
	}

	/**
	 * 提交订单明细 并跳转回订单详情页面
	 * 
	 * @param ids
	 * @param quatitys
	 * @param session
	 * @return
	 * @return
	 */
	@PostMapping("submitGoodsDetails")
	@ResponseBody
	public JSONObject submitGoods(HttpSession session) {
		CartBox cart = (CartBox) session.getAttribute("CartBoxAll");
		Integer oid = (Integer) session.getAttribute("ingOid");
		System.out.println("当前购物车有：" + cart);
		System.out.println("当前订单id是:" + oid);
		service.deleteOrderDetail(oid);// 删除对应订单id的订单详情
		service.addOrderDetail(oid, cart);// 添加对应订单id的订单详情
		JSONObject object = new JSONObject();
		object.put("odnum", oid);
		// session.removeAttribute("ingOid");
		session.removeAttribute("CartBoxAll");
		return object;
	}

	/**
	 * iframe跳出添加回款窗口
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("huikuanframe")
	public ModelAndView toHuikuan(Integer id, HttpSession session) {
		System.out.println("进来了orderForm2");
		System.out.println("订单的id:" + id);
		ModelAndView mv = new ModelAndView("/onSale/huikuanjilu");

		// 查询回款记录
		JSONObject findByOidItem = moneybackplanservice.findByOidItem(id);
		session.setAttribute("backText", findByOidItem);
		return mv;
	}

	/**
	 * 删除订单
	 * 
	 * @param id
	 * @param session
	 * @return
	 */
	@PostMapping("deleteOrderss")
	@ResponseBody
	public JSONObject deleteOrder(Integer id, HttpSession session) {
		System.out.println("进来了orderForm2");
		System.out.println("要删除的订单id:" + id);
		ModelAndView mv = new ModelAndView("/onSale/huikuanjilu");

		// 订单信息
		JSONObject findByIds = orderFormMapper.findByIds(id);
		service.delete(id);
		logforcustDAO.insert(findByIds.getInteger("cus_id"), new Date(), findByIds.getInteger("use_id"), 2203,
				findByIds.getInteger("o_id") + "订单已取消，并删除。", "");

		JSONObject js = new JSONObject();
		return js;
	}

	/**
	 * 添加汇款计划
	 * 
	 * @param cid
	 * @param oid
	 * @param havetime
	 * @param period
	 * @param percentage
	 * @param money
	 * @param user
	 * @param session
	 * @return
	 */
	@PostMapping("addBackMoney")
	@ResponseBody
	public int submitBackMoney(Integer cid, Integer oid, Date havetime, Integer period, Integer percentage,
			BigDecimal money, Integer user, Integer status, String deschui, HttpSession session) {
		System.out.println("客户：" + cid + " 订单id:" + oid + " 时间：" + havetime + " 期次：" + period + " 百分比：" + percentage
				+ " 金额：" + money + " 用户：" + user + " 状态：" + status + " 备注：" + deschui);
		// 添加一条回款记录
		moneybackplanservice.doinsertBack(cid, oid, havetime, period, percentage, money, user, status, deschui);
		int attribute = (int) session.getAttribute("orderidsTwo");
		if (period == 1) {
			logforcustDAO.insert(cid, new Date(), user, 2203, oid + "订单开始回款.", "");
			logforcustDAO.insert(cid, new Date(), user, 2203, oid + "订单第1期回款计划：" + money, "");

		} else {
			logforcustDAO.insert(cid, new Date(), user, 2203, oid + "订单第" + period + "期回款计划：" + money, "");

		}
		return attribute;
	}

	/**
	 * iframe跳出添加回款记录窗口
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("huikuanendframe")
	public ModelAndView toHuikuanend(Integer id, HttpSession session) {
		System.out.println("进来了orderForm2");
		System.out.println("回款记录的id:" + id);
		ModelAndView mv = new ModelAndView("/onSale/huikuanend");

		// 查询回款计划
		JSONObject findByOidItem = moneybackplanservice.findByMoneyBack(id);
		findByOidItem.put("jid", id);// 汇款计划id
		session.setAttribute("backEndText", findByOidItem);
		return mv;
	}

	/**
	 * 添加汇款记录
	 * 
	 * @param cid
	 * @param oid
	 * @param havetime
	 * @param period
	 * @param percentage
	 * @param money
	 * @param user
	 * @param session    paytypes jid status havetime deschui
	 * @return
	 */
	@PostMapping("addBackMoneyEnd")
	@ResponseBody
	public int submitBackMoneyEnd(Integer paytypes, Integer jid, Date havetime, Integer status, String deschui,
			HttpSession session) {
		System.out.println("回款方式：" + paytypes + " 计划id:" + jid + " 时间：" + havetime + " 是否开发票：" + status + " 备注："
				+ deschui + " 金额：");
		// 添加一条回款记录
		moneybackplanservice.inserBackEnd(paytypes, jid, havetime, status, deschui);
		int attribute = (int) session.getAttribute("orderidsTwo");

		// 订单信息
		JSONObject findByIds = orderFormMapper.findByIds(attribute);
		JSONObject findByOidItem = moneybackplanservice.findByMoneyBack(jid);
		logforcustDAO.insert(findByIds.getInteger("cus_id"), new Date(), findByIds.getInteger("use_id"), 2203,
				findByIds.getInteger("o_id") + "订单第" + findByOidItem.getString("mb_period") + "期回款计划 已回款..", "");

		// logforcustDAO.insert(cid, new Date(), user, 2203,oid
		// +"订单第"+period+"期回款计划："+money, "");
		// 开始判断订单是否已经全部完成
		orderfromServices.statusOrder(attribute);
		return attribute;
	}

	/**
	 * 删除回款计划
	 * 
	 * @param id
	 * @param session
	 * @return
	 */
	@PostMapping("deleteHuikuanjihua")
	@ResponseBody
	public int deletHuikuanjihua(Integer id, HttpSession session) {
		System.out.println("回款记录Idssss：" + id);
		// 添加一条回款计划
		moneybackplanservice.delteHuikuanjihua(id);

		int attribute = (int) session.getAttribute("orderidsTwo");
		return attribute;
	}

	/**
	 * 删除回款记录
	 * 
	 * @param id
	 * @param session
	 * @return
	 */
	@PostMapping("deleteHuikuanjilu")
	@ResponseBody
	public int deletHuikuanjilu(Integer id, HttpSession session) {
		System.out.println("回款记录Idssss：" + id);
		// 添加一条回款记录
		moneybackplanservice.delteHuikuanjilu(id);

		int attribute = (int) session.getAttribute("orderidsTwo");
		return attribute;
	}

	/**
	 * iframe跳出添加回款批次新建窗口
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("huikuanpiciframe")
	public ModelAndView toHuikuanpici(Integer id, HttpSession session) {
		System.out.println("进来了huikuanpici");
		System.out.println("回款记录的id:" + id);
		ModelAndView mv = new ModelAndView("/onSale/huikuanpici");
		JSONObject findOrderByID = orderfromServices.findOrderByID(id);
		mv.addObject("huikuanorder", findOrderByID);
		JSONArray array = new JSONArray();
		JSONObject obje = new JSONObject();
		obje.put("usen", findOrderByID.getString("use_name"));
		obje.put("used", findOrderByID.getInteger("use_id"));
		array.add(obje);
		mv.addObject("options", array);

		return mv;
	}

	/**
	 * 跳转到出库界面
	 * 
	 * @param oid
	 * @param session
	 * @return
	 */
	@RequestMapping("/shozhongorderthree")
	public ModelAndView goodsOut(Integer oid, HttpSession session) {
		System.out.println("来到出库");
		ModelAndView mv = new ModelAndView("onSale/shozhongorderthree");

		mv.addObject("deportText", depotgoodService.selectDeportGoods(oid));
		return mv;
	}

	/**
	 * 添加出库单
	 * 
	 * @param cid
	 * @param oid
	 * @param havetime
	 * @param period
	 * @param percentage
	 * @param money
	 * @param user
	 * @param session    paytypes jid status havetime deschui
	 * @return
	 */
	@PostMapping("/addOutDeport")
	@ResponseBody
	public int submitBackMoneyEnd(Integer oid, Integer deport, HttpSession session) {
		System.out.println("订单id：" + oid + " 仓库id:" + deport);

		int uid = (int) session.getAttribute("useid");
		depotgoodService.addOutDeportText(oid, deport, uid);
		return oid;
	}

}
